using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using NewGameTypes;
using UnityEngine;

public class GameModesUI : MonoBehaviour
{
	public enum TransportType
	{
		none = 0,
		car = 1,
		tank = 2,
		titan = 3
	}

	public static GameModesUI instance;

	private Dictionary<string, int> _likeModes = new Dictionary<string, int>();

	[SerializeField]
	private SelectableButtonControllerMode selectableButtonController;

	[SerializeField]
	private SelectableButtonMode exampleModeButton;

	[SerializeField]
	private UIScrollView scroll;

	[SerializeField]
	private GameObject scrollBar;

	[SerializeField]
	private UILabel countPlayersLable;

	[SerializeField]
	private UILabel descriptionLable;

	[SerializeField]
	private FriendsInSelectModeUI friendsPanel;

	[SerializeField]
	private LikeModeButton likeModeButton;

	[SerializeField]
	private UICenterOnChild centerOnChild;

	[SerializeField]
	private GameModeSelectUI controllerGameModeSelect;

	public NoticificationUI noticification;

	[Range(0f, 1f)]
	public float volume = 1f;

	[Range(0f, 2f)]
	public float pitch = 1f;

	public SelectableButtonControllerMode SelectableButtonController
	{
		get
		{
			return selectableButtonController;
		}
	}

	public static event Action OnStartMode;

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void OnEnable()
	{
		if ((bool)selectableButtonController && selectableButtonController.GetSelectableButton() != null)
		{
			selectableButtonController.GetSelectableButton().LikeModeButton.RefreshStatus();
			selectableButtonController.GetSelectableButton().LikeModeButton.RefreshView();
			selectableButtonController.GetSelectableButton().RefresfViewLikeBox();
			CenterOnSelectedButton();
		}
	}

	private void Start()
	{
		InitLikes();
		centerOnChild.onCenter = DisableCenterOnChild;
		StartCoroutine(InitModesButton());
	}

	public void Play()
	{
		controllerGameModeSelect.Show(selectableButtonController.GetSelectableButton());
	}

	public void StartMode()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		GameModeConfig.ModeInfo modeInfo = selectableButtonController.GetSelectableButton().ModeInfo;
		TypeGame typeGame = modeInfo.typeGame;
		switch (typeGame)
		{
		case TypeGame.race:
			if (settings.RaceCarName.Equals(string.Empty))
			{
				settings.RaceCarName = "_car_dodje_ChelendjerRace";
			}
			break;
		case TypeGame.tanks:
			if (settings.SelectedTankName.Equals(string.Empty))
			{
				settings.SelectedTankName = "_CarTankFree";
			}
			break;
		}
		if (GameModesUI.OnStartMode != null)
		{
			GameModesUI.OnStartMode();
		}
		if (VolumeController.IsSoundEnable)
		{
			BCWAudioResource.Instance.PlaySound(modeInfo.name, volume, pitch);
		}
		switch (typeGame)
		{
		case TypeGame.Offline:
			controllerMenu.thisScript.playOfflineMode();
			break;
		case TypeGame.RocketLeague:
		{
			if (!PhotonNetwork.connected && !IOSInternetConnection.isEnable)
			{
				controllerMenu.thisScript.ShowNoInternet();
			}
			int num = BCWStorage.Get<int>(settings.KEY_ROCKET_LEAGUE_PING_LIMIT);
			if (num <= 0)
			{
				num = 1000;
			}
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Clear();
			dictionary.Add("Player ping", FlurryWrapper.GetPingForFlurry(PhotonNetwork.GetPing()));
			FlurryWrapper.LogEvent(FlurryWrapper.EV_PING_ROCKET_LEAGUE, dictionary);
			if (PhotonNetwork.GetPing() > num)
			{
				noticification.Show(ScriptLocalization.Get("Game_Common/bad_ping"));
			}
			else
			{
				controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, typeGame);
			}
			break;
		}
		default:
			controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, typeGame);
			if (!PhotonNetwork.connected && !IOSInternetConnection.isEnable)
			{
				controllerMenu.thisScript.ShowNoInternet();
			}
			break;
		}
	}

	public void Show()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
		}
		else
		{
			base.gameObject.SetActive(true);
		}
	}

	public void Exit()
	{
		base.gameObject.SetActive(false);
		friendsPanel.gameObject.SetActive(false);
	}

	private void InitLikes()
	{
		foreach (GameModeConfig.ModeInfo item in GameModeManager.instance.ModesConfig)
		{
			int idModeByTypeGame = BCWServer.GetIdModeByTypeGame(item.typeGame);
			int value = (((bool)LikeModeManager.instance && LikeModeManager.instance.LikeModesRating.ContainsKey(idModeByTypeGame)) ? Mathf.Max(0, LikeModeManager.instance.LikeModesRating[idModeByTypeGame]) : 0);
			_likeModes.Add(item.name, value);
		}
	}

	private IEnumerator InitModesButton()
	{
		List<GameModeConfig.ModeInfo> modes = new List<GameModeConfig.ModeInfo>(GameModeManager.instance.ModesConfig);
		if (CupsManager.CurrentLeague != 0 && CupsManager.CurrentLeague != League.none)
		{
			modes.Sort(delegate(GameModeConfig.ModeInfo x, GameModeConfig.ModeInfo y)
			{
				if (x.isNew && !y.isNew)
				{
					return -1;
				}
				if (x.isNew == y.isNew)
				{
					if (_likeModes[x.name] > _likeModes[y.name])
					{
						return -1;
					}
					if (_likeModes[x.name] == _likeModes[y.name])
					{
						return 0;
					}
					return 1;
				}
				return 1;
			});
		}
		else
		{
			modes.Sort(delegate(GameModeConfig.ModeInfo x, GameModeConfig.ModeInfo y)
			{
				if (x.order > y.order)
				{
					return 1;
				}
				return (x.order != y.order) ? (-1) : 0;
			});
		}
		foreach (GameModeConfig.ModeInfo modeInfo in modes)
		{
			SelectableButtonMode tmpButton = UnityEngine.Object.Instantiate(exampleModeButton);
			tmpButton.ModeInfo = modeInfo;
			tmpButton.gameObject.SetActive(true);
			tmpButton.transform.SetParent(selectableButtonController.grid.transform);
			tmpButton.transform.localScale = Vector3.one;
			tmpButton.CountPlayersLable = countPlayersLable;
			tmpButton.DescriptionLable = descriptionLable;
			likeModeButton.Status = (bool)LikeModeManager.instance && LikeModeManager.instance.GetLikeStatus(modeInfo.typeGame);
			tmpButton.LikeModeButton = likeModeButton;
			tmpButton.Init();
			tmpButton.SetLikePersent(_likeModes.ContainsKey(modeInfo.name) ? _likeModes[modeInfo.name] : 0);
			selectableButtonController.AddNewButton(tmpButton);
		}
		if (selectableButtonController.allButtons[0] != null)
		{
			selectableButtonController.allButtons[0].Select(true);
		}
		if (GameModeManager.instance.NeedSoonButton)
		{
			GameObject buttonOff = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("Prefabs/UI_SelectMode/UnSelectMode/ModeButton_Off"));
			buttonOff.transform.SetParent(selectableButtonController.grid.transform);
			buttonOff.transform.localScale = Vector3.one;
		}
		selectableButtonController.grid.maxPerLine = GameModeManager.instance.GridMaxPerLine;
		selectableButtonController.grid.onReposition = CenterOnSelectedButton;
		selectableButtonController.grid.repositionNow = true;
		yield return null;
	}

	private void CenterOnSelectedButton()
	{
		centerOnChild.Recenter();
		scroll.ResetPosition();
		if (selectableButtonController.selectedButton != null)
		{
			int num = selectableButtonController.allButtons.IndexOf(selectableButtonController.selectedButton);
			if (num >= selectableButtonController.grid.maxPerLine * 2)
			{
				centerOnChild.CenterOn(selectableButtonController.selectedButton.transform);
			}
		}
	}

	private void DisableCenterOnChild(GameObject go = null)
	{
		centerOnChild.enabled = false;
	}
}
